<template>
	<view class="container">
		<view class="member-box">
			<view class="title">
				<view class="title-left">守护成员</view>
				<view class="title-right">(共{{memberList.length}}人)</view>
			</view>
			<view class="list">
				<view class="item" v-for="(item,index) in memberList" :key="item.Id">
					<view class="img-box">
						<image class="img" src="../../static/img/2.png"></image>
					</view>
					<view class="item-name">姓名：{{item.RealName}}</view>
					<view class="item-relation">{{item.ClassName}}</view>
				</view>
				<view class="item" @tap="showAdd">
					<view class="img-box">
						<view class="img-bg">
							<view class="plus">+</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="add-bg" v-show="isShowAdd" @tap="hideAdd">
			<view class="add-container" @tap.stop="">
				<view class="add-item">
					<view class="item-label">姓<view class="blank"></view>名:</view>
					<view class="item-input-box">
						<input class="item-input" v-model="realName" />
					</view>
				</view>
				<view class="add-item">
					<view class="item-label">身份证号:</view>
					<view class="item-input-box">
						<input class="item-input" v-model="idCard" />
					</view>
				</view>
				<view class="add-submit">
					<view class="add-submit-label" @tap="doSubmit">提交</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import {myRequest,showToastMsg} from '../../utils'
	
	export default {
		data() {
			return {
				loading: false,
				isShowAdd: false,
				memberList: [],
				timer: null,
				realName: '',
				idCard: '',
				idCardReg: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
			}
		},
		
		onShow() {
			this.fetchData()
		},
		
		methods: {
	
			showAdd() {
				this.isShowAdd = true
			},
			
			hideAdd() {
				this.isShowAdd = false
			},
			
			fetchData() {
				
				let that = this
				if (that.loading) return
				that.loading = true
				
				uni.showLoading({
					title: '拼命加载中...',
					success() {
						
						if (that.timer != null) {
							clearTimeout(that.timer)
						}
						
						that.timer = setTimeout(function() {
							
							myRequest({
								url: '/api/student/children_member',
								data: {},
							}, function(res) {
								
								that.loading = false
								uni.hideLoading()
								
								if (res.statusCode != 200) {
									uni.showModal({
										title: '提示',
										content: '服务器错误，请求失败'
									})
									return
								}
								
								if (res.data.status != 0) {
									uni.navigateTo({
										url: '/pages/login/login'
									})
									return
								}
								
								that.memberList = res.data.data
								
							})
							
						}, 1000)
						
					}
				})
				
			},
			
			doSubmit() {
				
				let that = this
				
				if (that.realName.trim().length <= 0) {
					showToastMsg('请输入姓名')
					return
				}
				if (that.idCard.trim().length <= 0) {
					showToastMsg('请输入身份证号')
					return
				}
				if (!that.idCardReg.test(that.idCard)) {
					showToastMsg('请输入正确的身份证号')
					return
				}
				
				if (that.loading) return
				that.loading = true
				
				uni.showLoading({
					title: '拼命提交中...',
					success() {
						
						if (that.timer != null) {
							clearTimeout(that.timer)
						}
						
						that.timer = setTimeout(function() {
							
							myRequest({
								url: '/api/student/add_children_member',
								data: {
									realName: that.realName,
									idCard: that.idCard
								},
							}, function(res) {
								
								that.loading = false
								uni.hideLoading()
								
								if (res.statusCode != 200) {
									uni.showModal({
										title: '提示',
										content: '服务器错误，请求失败'
									})
									return
								}
								
								if (res.data.status == 1000) {
									uni.navigateTo({
										url: '/pages/login/login'
									})
									return
								}
								
								showToastMsg(res.data.msg)
								
								if (res.data.status == 0) {
									that.hideAdd()
									that.fetchData()
								}
								
							})
							
						}, 1000)
						
					}
				})
				
			}
	
		}
	}
</script>

<style>
	.container {
		width: 750rpx;
		height: auto;
		background-image: linear-gradient(#ffae00, #eee, #efefef);
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-bottom: 160rpx;
		min-height: 1800rpx;
	}
	
	.member-box {
		width: 600rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.title {
		width: 100%;
		display: flex;
		padding-left: 70rpx;
	}
	
	.title-left {
		font-size: 50rpx;
	}
	
	.title-right {
		font-size: 32rpx;
		margin-left: 20rpx;
		margin-top: 12rpx;
	}
	
	.list {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		width: 100%;
	}
	
	.item {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 176rpx;
		height: 248rpx;
		background-color: #ffffff;
		border-radius: 32rpx;
		margin-top: 70rpx;
		position: relative;
		font-size: 26rpx;
	}
	
	.img-box {
		width: 112rpx;
		height: 112rpx;
		border-radius: 50%;
		background-color: #ffffff;
		position: absolute;
		top: -40rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 50%;
		overflow: hidden;
		border: 8rpx solid #ffffff;
	}
	
	.img {
		width: 112rpx;
		height: 112rpx;
	}
	
	.item-name {
		padding-left: 16rpx;
		width: 100%;
		margin-top: 136rpx;
	}
	
	.item-relation {
		padding-left: 16rpx;
		width: 100%;
	}
	
	.img-bg {
		width: 94rpx;
		height: 94rpx;
		background-color: #fec901;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.plus {
		color: #ffffff;
		font-size: 80rpx;
		text-shadow: 2rpx 2rpx 0px rgba(145, 124, 24, 0.49);
		position: relative;
		top: -6rpx;
	}
	
	.add-bg {
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 100;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.add-container {
		width: 78%;
		background-color: #fec901;
		border-radius: 28rpx;
		z-index: 200;
		display: flex;
		flex-direction: column;
		padding-top: 40rpx;
		padding-bottom: 40rpx;
		padding-left: 56rpx;
		padding-right: 56rpx;
	}
	
	.add-item {
		height: 70rpx;
		background-color: #ffffff;
		display: flex;
		align-items: center;
		font-size: 28rpx;
		z-index: 300;
		border-radius: 6rpx;
		margin-bottom: 20rpx;
		box-shadow: 2rpx 6rpx 18rpx 0px rgba(167, 167, 167, 0.71);
	}
	
	.item-label {
		width: 25%;
		text-align: end;
		z-index: 400;
	}
	
	.item-input-box {
		width: 68%;
		z-index: 400;
		margin-left: 30rpx;
		height: 100%;
	}
	
	.item-input {
		height: 100%;
	}
	
	.add-submit {
		display: flex;
		justify-content: center;
		margin-top: 30rpx;
	}
	
	.add-submit-label {
		width: 160rpx;
		height: 70rpx;
		background-color: #ffffff;
		box-shadow: 2rpx 6rpx 18rpx 0px rgba(167, 167, 167, 0.71);
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 6rpx;
		font-size: 32rpx;
	}
	
	.blank {
		display: inline-block;
		width: 50rpx;
	}
</style>
